package com.tsugun.mediaplayer.view.fragment.subfragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.AlbumModel;
import com.tsugun.mediaplayer.presenter.manager.MusicFragmentManager;
import com.tsugun.mediaplayer.view.activity.IndexActivity;
import com.tsugun.mediaplayer.view.adapter.AlbumItemAdapter;
import com.tsugun.mediaplayer.view.fragment.AlbumFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * create an instance of this fragment.
 */
public class AlbumListFragment extends SubBaseFragment {

    @BindView(R.id.id_list_recycler)
    RecyclerView mRvAlbumList;

    private View mFragment;
    private Context mContext;
    private Unbinder unbinder;
    private IndexActivity indexActivity;
    private MusicFragmentManager mManager;

    private List<AlbumModel> mResult;
    private RecyclerView.LayoutManager mLayoutManager;
    private boolean isFirst;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isFirst = true;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mFragment = inflater.inflate(R.layout.fragment_list, container, false);
        mContext = container.getContext();
        unbinder = ButterKnife.bind(this, mFragment);

        initData();
        bindData();
        return mFragment;
    }

    /**
     * 初始化数据
     */
    private void initData() {
        indexActivity = (IndexActivity) mContext;
        mManager = indexActivity.getMusicFragmentManager();
        mLayoutManager = new GridLayoutManager(mContext, 3);
    }

    /**
     * 绑定数据
     */
    private void bindData() {
        initList();
    }

    /**
     * 添加结果集合
     *
     * @param result list<AlbumModel>
     */
    public void setResult(List<AlbumModel> result) {
        if (mResult == null) {
            mResult = result;
            return;
        }
        mResult = result;
        initList();
    }

    /**
     * 初始化列表
     */
    private void initList() {
        AlbumItemAdapter adapter = new AlbumItemAdapter(mContext, mResult);
        adapter.setOnAlbumItemClickListener(new AlbumItemAdapter.OnAlbumItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Bundle bundle = new Bundle();
                bundle.putString(AlbumFragment.INTENT_STRING_ALBUM_TITLE, mResult.get(position).getName());
//                musicFragmentManager.addSharedElement(v.findViewById(R.id.id_item_rounded_image));
                AlbumFragment fragment = new AlbumFragment();
                fragment.setArguments(bundle);
                mManager.openFragment(fragment);
            }
        });
        mRvAlbumList.setAdapter(adapter);
        mRvAlbumList.setLayoutManager(mLayoutManager);
        if (!isFirst) {
            mRvAlbumList.startLayoutAnimation();
        }
        isFirst = false;
        setSettingTheme(mRvAlbumList);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
